package com.hq.fxh.No6UerSpace.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hq.fxh.CustomerView.CircleImageView;
import com.hq.fxh.CustomerView.NoScrollViewPager;
import com.hq.fxh.CustomerView.countview.RiseNumberTextView;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Intefers.NetworkSubscriber;
import com.hq.fxh.Modle.GetUserNumData;
import com.hq.fxh.Modle.SelectByMemberIdRes;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item.ToastUtils;
import com.hq.fxh.No4_XiaoXi.IM.LiaoTianActivity;
import com.hq.fxh.No6UerSpace.Adapter.Padpter_Space;
import com.hq.fxh.No6UerSpace.Fragment.Fragment_JianLi;
import com.hq.fxh.No6UerSpace.Fragment.Fragment_UserSpace_Plate;
import com.hq.fxh.No6UerSpace.Fragment.Fragment_UserSpace_Video;
import com.hq.fxh.R;
import com.hq.fxh.Utils.AppConstant;
import com.hq.fxh.Utils.GlideUtils;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.StatesBarUtils;
import com.hq.fxh.Utils.TabUtils;
import com.hq.fxh.Utils.Tool;

import java.util.Arrays;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.UserInfo;
import rx.Subscriber;

import static cn.jpush.im.android.tasks.GetUserInfoListTask.IDType.uid;
import static com.baidu.location.h.j.ar;
import static com.tencent.download.global.Global.getContext;


public class Space_UserActivity extends AppCompatActivity implements Padpter_Space.TabPagerListener, View.OnClickListener {

    protected CircleImageView imageUserSpaceHeadPic;
    protected TextView tvUserSpaceName;
    protected TextView tvUserSpaceJieShao;
    protected TextView tvUserSpaceGuanZhu;
    protected LinearLayout layoutSpaceUserGuanZhu;
    protected LinearLayout layoutSpaceUserLiaoTian;
    protected RiseNumberTextView tvSpaceTieNum;
    protected LinearLayout layoutSpaceTie;
    protected RiseNumberTextView tvSpaceGuanzhuNum;
    protected LinearLayout layoutSpaceGuanzhu;
    protected RiseNumberTextView tvSpaceFensiNum;
    protected LinearLayout layoutSpaceFenSi;
    Toolbar toolbar;
    CollapsingToolbarLayout collapsingToolbar;
    TabLayout tabLayout;
    AppBarLayout appbar;
    ViewPager viewpager;
    int authorID;

    private Padpter_Space adapter;
    private Fragment_UserSpace_Plate fragment_userSpace_plate;
    private Fragment_UserSpace_Video fragment_userSpace_video;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       /* //当系统版本为4.4或者4.4以上时可以使用沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }*/
        super.onCreate(savedInstanceState);
        StatesBarUtils.setWindowToWhite(this);
        super.setContentView(R.layout.activity_main2);
        initView();
        initData();


    }

    private void initData() {
        authorID = getIntent().getIntExtra("uid", 0);
        if (authorID == 0) {
            LogUtils.startToash(Space_UserActivity.this, "接受数据失败");
            finish();

        }

        getMerchantDetails(authorID);
        getUserData(authorID);
        collapsingToolbar.setBackgroundResource(R.color.white);
        collapsingToolbar.setExpandedTitleColor(Color.parseColor("#00ffffff"));//设置还没收缩时状态下字体颜色
        collapsingToolbar.setCollapsedTitleGravity(Gravity.CENTER);
        adapter = new Padpter_Space(getSupportFragmentManager()
                , 2, Arrays.asList("帖子", "视频", "简历"), this);
        adapter.setListener(this);
        viewpager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewpager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtils.startToash(Space_UserActivity.this, "nava监听");
                finish();
            }
        });
        TabUtils.setIndicator(tabLayout, 50, 50);
    }

    @Override
    public Fragment getFragment(int position) {
        switch (position) {
            case 0:
                fragment_userSpace_plate = Fragment_UserSpace_Plate.newInstance();
                fragment_userSpace_plate.setMemberId(authorID);
                return fragment_userSpace_plate;

            case 1:
                fragment_userSpace_video = Fragment_UserSpace_Video.newInstance(1);
                fragment_userSpace_video.setUid(authorID);
                return fragment_userSpace_video;

            default:
                return Fragment_JianLi.newInstance();

        }
    }

    private void initView() {
        viewpager = (NoScrollViewPager) findViewById(R.id.viewpager);
        imageUserSpaceHeadPic = (CircleImageView) findViewById(R.id.image_UserSpace_HeadPic);
        tvUserSpaceName = (TextView) findViewById(R.id.tv_UserSpace_Name);
        tvUserSpaceJieShao = (TextView) findViewById(R.id.tv_UserSpace_JieShao);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsingToolbar);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        appbar = (AppBarLayout) findViewById(R.id.appbar);
        tvUserSpaceGuanZhu = (TextView) findViewById(R.id.tv_UserSpace_GuanZhu);
        layoutSpaceUserGuanZhu = (LinearLayout) findViewById(R.id.layout_spaceUser_GuanZhu);
        layoutSpaceUserGuanZhu.setOnClickListener(Space_UserActivity.this);
        layoutSpaceUserLiaoTian = (LinearLayout) findViewById(R.id.layout_spaceUser_LiaoTian);
        layoutSpaceUserLiaoTian.setOnClickListener(Space_UserActivity.this);
        tvSpaceTieNum = (RiseNumberTextView) findViewById(R.id.tv_Space_TieNum);
        layoutSpaceTie = (LinearLayout) findViewById(R.id.layout_Space_Tie);
        layoutSpaceTie.setOnClickListener(Space_UserActivity.this);
        tvSpaceGuanzhuNum = (RiseNumberTextView) findViewById(R.id.tv_Space_guanzhuNum);
        layoutSpaceGuanzhu = (LinearLayout) findViewById(R.id.layout_Space_Guanzhu);
        layoutSpaceGuanzhu.setOnClickListener(Space_UserActivity.this);
        tvSpaceFensiNum = (RiseNumberTextView) findViewById(R.id.tv_Space_fensiNum);
        layoutSpaceFenSi = (LinearLayout) findViewById(R.id.layout_Space_FenSi);
        layoutSpaceFenSi.setOnClickListener(Space_UserActivity.this);


    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.layout_spaceUser_GuanZhu) {

        } else if (view.getId() == R.id.layout_spaceUser_LiaoTian) {
            toLiaoTianativity();

        } else if (view.getId() == R.id.layout_Space_Tie) {

        } else if (view.getId() == R.id.layout_Space_Guanzhu) {

        } else if (view.getId() == R.id.layout_Space_FenSi) {

        }
    }

    public void toLiaoTianativity() {
        LogUtils.e(" 账号id" + authorID);
        if (authorID == 0) {


            LogUtils.startToash(Space_UserActivity.this, " 未获取到该聊天对象");
        } else {
            JMessageClient.getUserInfo(authorID + "", new GetUserInfoCallback() {
                @Override
                public void gotResult(int i, String s, UserInfo userInfo) {
                    if (i == 0) {
                        Intent intent = new Intent(Space_UserActivity.this, LiaoTianActivity.class);
                        intent.putExtra("user", authorID + "");
                        startActivity(intent);
                    } else {
                        LogUtils.startToash(Space_UserActivity.this, AppConstant.userNoIM);

                    }
                }
            });


        }

    }

    public void getUserData(int uid) {
        UserServiceImpl.getUserData(uid, new Subscriber<GetUserNumData>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(GetUserNumData responseBody) {

                GetUserNumData.DataBean data = responseBody.getData();
                if (data == null) {

                } else {
                 /*   data.get*/
                }

            }
        });
    }


    public void getMerchantDetails(int memberId) {
        if (memberId != 0) {
            UserServiceImpl.selectByMemberId(memberId, new NetworkSubscriber<SelectByMemberIdRes>() {
                @Override
                public int onSuccess(SelectByMemberIdRes data) {
                    super.onSuccess(data);
                    if (data != null && data.getCode() == 0) {


                        String faceUrl = data.getData().getFace();
                        if (!TextUtils.isEmpty(faceUrl)) {
                            String face = data.getData().getFace();
                            String nickname = data.getData().getNickname();
                            tvUserSpaceName.setText(nickname);
                            Tool.setUrlToImage(Space_UserActivity.this, face, imageUserSpaceHeadPic);

                        }
                        String name = data.getData().getNickname();
                    } else {
                        ToastUtils.show(Space_UserActivity.this, data.getMsg());
                    }
                    return 0;
                }
            });
        } else {
            initData();
        }
    }
}
